package com.ding.custommanagersys.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ding.custommanagersys.bo.AreaBo;
import com.ding.custommanagersys.bo.EmailBo;
import com.ding.custommanagersys.entity.Area;
import com.ding.custommanagersys.entity.Email;
import com.ding.custommanagersys.entity.ResultData;
import com.ding.custommanagersys.entity.User;
import com.ding.custommanagersys.service.IEmailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ding
 * @since 2023-04-21
 */
@RestController
@RequestMapping("/email")
public class EmailController {

    @Autowired
    private IEmailService emailService;

    @PostMapping("/sendEmail")
    public ResultData<Integer> sendEmail(@RequestBody EmailBo bo){
        ResultData<Integer> resp = new ResultData<>();
        Integer num = emailService.sendEmail(bo);
        if(num == 0){
            resp.error("发送失败，请联系管理员！");
        }else{
            resp.ok();
        }
        return resp;
    }

    @RequestMapping("/getAllEmail")
    public ResultData<List<Email>> getAllEmail(EmailBo bo, HttpSession session){
        ResultData<List<Email>> resp = new ResultData<>();
        User user = (User) session.getAttribute("user");
        bo.setCustomId(user.gettUserId());
        Page<Email> page = emailService.getAllEmail(bo);
        resp.setData(page.getRecords());
        resp.setCount(page.getTotal());
        return resp.ok();
    }


    /**
     * 删除 短信内容
     */
    @PostMapping("/deleteEmail")
    public ResultData<Integer> addArea(@RequestBody EmailBo bo, HttpSession session){
        User user = (User) session.getAttribute("user");
        ResultData<Integer> resp = new ResultData<>();
        Integer num = emailService.deleteEmail(bo.getIds(),user.gettUserId());
        if(num != 0){
            resp.ok();
        }else{
            resp.error("删除失败，请联系管理员");
        }
        return resp;
    }
}
